
function updateTips( t , append , fromProfile)
{
    var tipText = t;
	if(append)
	{
        tipText = $("#ui-tips").text() + "\n" + tipText;
    }
	if($(".ketchup-error").length==0){
		$("#ui-tips")
	    .text( tipText )
	    .addClass( "ui-state-highlight").css({'background':'#f2dede','border-color':'#f2dede'});
	}
	if(fromProfile==true){
		$("#ui-tips")
	    .text( tipText )
	    .addClass( "ui-state-highlight" ).css({'background':'#f2dede','border-color':'#f2dede'});
	}
	
}

function checkLength( o, n, min, max ) {
    if ( o.val().length > max || o.val().length < min ) {
        o.css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
//        o.addClass( "ui-state-error" );
        updateTips( "Length of " + n + " must be between " +
            min + " and " + max + "." );
        return false;
    } else {
        return true;
    }
}

function checkMandatory(fieldId, fieldName, appendError){
	if($(fieldId).length > 0)
	{
    var fieldIdtrim=$.trim($(fieldId).val());
	
	//if($(fieldId).val().trim().length < 1){
	if(fieldIdtrim.length < 1){
        $(fieldId).addClass("error");
	
        //updateTips(fieldName + " is Required.", appendError, true);
        return false;
    }
	}
    return true;
}

function checkRegexp( o, regexp, n ) {
    if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
    } else {
        return true;
    }
}



function callQglAction(location){
	window.location = location;
}


function updateTipsForPageCalculation( t , append )
{
    var tipText = t;
	
	if(append)
	{
        tipText = $("#ui-tips").text() + "\n" + tipText;
    }
		
    $("#ui-tips")
        .text( tipText )
        .addClass( "ui-state-highlight" );
//    setTimeout(function() {
//        $("#ui-tips").removeClass( "ui-state-highlight", 1500 );
//    }, 500 );
}


/*------------ help section--------------------------*/

	$(document).ready(function ()
	{ 
//	  $('.mainLinks').click(function(e){
//		    e.preventDefault();
//			saveOrDiscard($(this).attr('id'));
//
//	  });
	   
	  
	   
	  
	  
	  
	$("input:checkbox").live("focusin",function(){
	$(this).parent().css("background","#FFF9B8");
	});
	$("input:checkbox").live("focusout",function(){
	$(this).parent().css("background","none");
	});
		$.ketchup.validation('price', 'Enter Valid Value', function(form, el, value) {
			
		var flag=false;
		//var isCurrency_re    = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/;
		//var isCurrency_re    = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d))\s*$/;
		var  isCurrency_re =  /^[-]?\d*\.?\d*$/;
		
		if(isNaN(value)){
			flag = false;
		}
		else
        flag=isCurrency_re.test(value);
		
		if(flag && value==0){flag=false;}
	     return flag;
			});




			$.ketchup.validation('phone', 'Enter Valid Phone number', function(form, el, value) {
				
			var flag=false;
  		//var phone1=/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
  
		var phone1=/^\(?[0-9]?[-.+ ]?([0-9]{0,9})\)?[-. ]?([0-9]{2,9})[-. ]?([0-9]{3,8})$/;
		var eleVal =value.replace(/^\s+|\s+$/g,"");
			if(eleVal=="")
			flag=true;
			else if(phone1.test(eleVal)&&eleVal.length > 9&&eleVal.length < 16)
			flag=true;
		
			//alert(zipcode_regex.test(eleVal) + "" + flag);}
			
			return flag;
		
			});
			$.ketchup.validation('zip', 'Enter Valid ZIP CODE', function(form, el, value) {
				

			var flag=false;
			var eleval = value.replace(/^\s+|\s+$/g,"");
			var zipcode_regex = /^\d{5}$/;
			var zip_canada=/^[a-z][0-9][a-z]\s*?[0-9][a-z][0-9]$/i;
			var zipUS=/^(\d{5})(-\d{4})?$/;
		
			if(eleval=="")
			flag=true;
			else if(zipcode_regex.test(eleval))
			flag=true;
			else if(zip_canada.test(eleval))
			flag=true;
			else if(zipUS.test(eleval))
			flag=true;
			//alert(zipcode_regex.test(eleVal) + "" + flag);}
			
		return flag;
		
			});



			$.ketchup.validation('curl', 'Must be Valid url', function(form, el, value) {
			var pattern = /[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/;
			
			 if (pattern.test(value)) {
           
            return true;
				} 
            
            return false;


			});
	
       $("#helpContainer").css({marginRight:"-1000px"});
	   $("#helpContainer").width("0px")
	  
	   $(function () {
       $("#close,#slidebutton").click(function () {
	   $("#helpContainer").width("0px")
       $("#helpContainer").animate({marginRight:"-1000px"},1000);
     });
           
      $("#helpButton").click(function () {
		  //$("#helpContainer").css("display","block");
	  $("#helpContainer").width("400px")
	  $("#helpContainer").animate({marginRight:"0px"},500);
	  });
      });
	   
	  $("#infoContainer").css({marginRight:"-1000px"});
	   $("#infoContainer").width("0px");
	   $("#qglRTinfoContainer").css({marginRight:"-1000px"});
	   $("#qglRTinfoContainer").width("0px");
	   
	$(function(){
		$("#infoclose,#slidebutton1").click(function () {
	   $("#infoContainer").width("0px")
       $("#infoContainer").animate({marginRight:"-1000px"},1000);
	   //$("#helpIMG").css("display","block");
		});
	$("#infoButton").click(function(){
		$("#infoContainer").css("display","block");
		 $("#infoContainer").width("400px")
		 $("#infoContainer").animate({marginRight:"0px"},500);
		 //$("#helpIMG").css("display","none");
	});
	  $("#qglRTinfoclose, #qglRTslidebutton1").click(function () {
			 $("#qglRTinfoContainer").width("0px")
     $("#qglRTinfoContainer").animate({marginRight:"-1000px"},1000);
			 $("#qglRTinfoIMG").css("display","block");
    
  });
  $("#qglRTinfoButton").click(function () {
			 $("#qglRTinfoContainer").css("display","block");
			 $("#qglRTinfoContainer").width("400px")
		      $("#qglRTinfoContainer").animate({marginRight:"0px"},500);
			 $("#qglRTinfoIMG").css("display","none");
			 
		});
      
	});
	 
	   
	});
	 

 $.fn.customValidator=function(validatestring){
	
  $(this).live("blur",function(){
		if($(this).val().length>0){
		  $(this).attr("data-validate",validatestring);
		  }
		 else {$(this).removeAttr("data-validate");
		 		}
		 $("form").ketchup();

		});};
		
var isProfileChanged = false;//it will become true if any change happens in profile page
var isRateTableChaned = false;//it will become true if any change happens in ratetable page.
var isPendingChanged = false;//it will become true if any change happens in qgl page.
var isDetailAccountingChanged = false;
var isFormFieldChanged = false;//it will become true if any change happens in any one of the above page.
var defaultAction;//if no change happens in any page then page will redirect to this action




/**if currentAction is 0 then we are in QGL page.
 *if currentAction is 1 then we are in RateTable page.
 *if currentAction is 2 then we are in Profile page.
 *if currentAction is 3 then we are in Billing page.
 *The value for this variable will be set while page loading*/
var currentAction;
var currentAction1;

var nextAction;

/**
 * this variable indicates the action need to submit(i.e currentAction) when 
 * we move to another tab without saving the form after changed the form fields.
 */
var submitAction;
var currentActionID;

/**
 * this variable indicates the form need to be submitted.
 */
var formId;

function saveOrDiscard(actionId){
	
	/*alert(currentAction);


	debugger;
	*/
	if(actionId == 0){
		nextAction = "/qgl-log-pending.action";
		defaultAction = "/quiggle/qgl-log-pending.action";
	}
	else if(actionId == 1){
		nextAction = "/rate-template.action";
		defaultAction = "/quiggle/rate-template.action";

	}
	else if(actionId == 2){
		nextAction = "/my-profile.action";
		defaultAction = "/quiggle/my-profile.action";
	}
	else if(actionId == 3){
		nextAction = "/billing-receivable.action";
		defaultAction = "/quiggle/billing-receivable.action";

	}
	
	
	
	if(currentAction > 4){
		
		window.location.href=defaultAction;
		return false;
		
	}
	
	/*if(currentActionID == 0)
	{
		currentAction = 0;
		isPendingChanged = true;
	}*/
	if(currentAction == 0)
		submitAction = "/quiggle/qgl-log-pending.action";
	else if(currentAction == 1)
		submitAction = "/quiggle/rate-template.action";
	else if(currentAction == 2)
		submitAction = "/quiggle/my-profile.action";
	else if(currentAction == 3)
		submitAction = "/quiggle/billing-receivable.action";
	
	/*else if(currentAction == 4)
		submitAction = "/quiggle/qgl-log-detail-accounting.action";*/
	
	
	if(isFormFieldChanged == false )
		window.location.href=defaultAction;
	
	if(currentAction == 0){
	     formId ="#job-template-0";
		//window.location.href=defaultAction;
		if(isPendingChanged == true)
			redirectDialog(formId,submitAction,nextAction);		
		else
			window.location.href=defaultAction;
			
	}
	else if(currentAction == 1){
		formId = "#rateTableFormId";
		if(isRateTableChaned == true)			
			redirectDialog(formId,submitAction,nextAction);		
	}
	else if(currentAction == 2){
		formId = "#my-profile-form";
		if(isProfileChanged == true)			
			redirectDialog(formId,submitAction,nextAction);
	}else if(currentAction == 3){
		//currentAction1= "/quiggle/billing-receivable.action";
		window.location.href=defaultAction;
	}
	else
		window.location.href=defaultAction;/*else if(currentAction == 4){
		formId = "#detail-accounting-0";
		
		if(isDetailAccountingChanged == true)
			redirectDialog(formId,submitAction,nextAction);
		//currentAction1 = "/quiggle/qgl-log-detail-accounting.action";
		//window.location.href=defaultAction;
	}*/
}

function redirectDialog(formId,currentAction,nextAction){	
	
	
	$('#save-or-discard').html("SAVE BEFORE LEAVING THIS PAGE?");
	
    $("#save-or-discard" ).dialog({
		height: 160,
		width: 340,
		modal: true,
		title:"Save or Discard",
		hide: 'fold',
        show: 'blind',
		buttons: {
			"Yes": function() {				
				$(formId).attr("action", currentAction+"?saveAndNextAction=&nextAction="+nextAction); 
				$(formId).attr("method","post"); 
				$(formId).submit();
			},
			"No": function() {
				isProfileChanged =false;
				isFormFieldChanged=false;
				isRateTableChaned=false;
				window.location.href=defaultAction;				
				$(this).dialog('close');
			},
			"Cancel": function() {
				$(this).dialog('close');
			}
		}
	});
}



/*$(document).ready(function () {   
    $("#infoContainer").css({marginRight:"-1000px"});
	   $("#infoContainer").width("0px")
	  
		  $(function () {
         $("#infoclose").click(function () {
				 $("#infoContainer").width("0px")
            $("#infoContainer").animate({marginRight:"-1000px"},1000);
				 $("#helpIMG").css("display","block");
           
         });
         $("#infoButton").click(function () {
				 $("#infoContainer").width("400px")
			      $("#infoContainer").animate({marginRight:"0px"},500);
				 $("#helpIMG").css("display","none");
				 
			});
      });

	$(".instructionsHideLinkForQGL").live("click",function(){

		  $(".contentInsDiv").hide() 
		   $(".instrutionsSpanForQGL").html("<a title='click here to show the instructions' class='instructionsShowLinkForQGL'  style='background:#E3F2FF;' href='javascript:{}'><img src='images/downarrow.png' width='10' height='9' alt='Hide' /></a>");
		});

		$(".instructionsShowLinkForQGL").live("click",function(){
			
			  $(".contentInsDiv").show();
			$(".instrutionsSpanForQGL").html("<a title='click here to hide the instructions' class='instructionsHideLinkForQGL' style='background:#E3F2FF;' href='javascript:{}'><img  id='instructionsuparrowimg' src='images/uparrow.png' width='10' height='9' alt='Hide' /></a>");

		});
	});*/
	
